/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.util;

import cn.ac.iie.di.commons.speeder.SpeedMonitor;
import cn.ac.iie.di.rocketmq.RocketMqClient;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.REMessage;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import org.apache.log4j.Logger;

/**
 *
 * @author YG
 */
public class GlobalParas {

    public static Logger log = Logger.getLogger(GlobalParas.class.getName());
    public static ConfigureUtil cfg = null;

    public static SpeedMonitor spm = null;

    public static String nameServer = "";
    public static String[] oriMsgMqs = null;
    public static boolean ifFromTimeStamp2Consumer = false;
    public static String consumeTimeStamp = "";

    public static ArrayBlockingQueue<RocketMqClient> rocketmqclientQueue ;
    public static int oriMsgPoolSize = 0;

    public static ArrayBlockingQueue<REMessage> oriMsgPool = null;
    public static String localPath = "";
    public static String packPath = "";
    public static int writeTimeOut = 0;
    public static int writeFileSize = 0;
    public static int writeThreadSize = 0;

    public static int intervalSec = 1;
    public static int delaySec = 1;

    public static volatile boolean isShouldExit = false;

    public static void init() {
        try {
            cfg = new ConfigureUtil(ClassLoader.getSystemClassLoader().getResource("config.properties").getFile());
        } catch (Exception e) {
            log.error(e.getMessage() + ",the program will exit", e);
            return;
        }

        GlobalParas.nameServer = cfg.getProperty("nameServer");
        GlobalParas.ifFromTimeStamp2Consumer = cfg.getBooleanProperty("ifFromTimeStamp2Consumer");
        GlobalParas.consumeTimeStamp = cfg.getProperty("consumeTimeStamp");
        GlobalParas.oriMsgMqs = cfg.getProperty("oriMsgMqs").split("[|]");
        GlobalParas.rocketmqclientQueue = new ArrayBlockingQueue<>(GlobalParas.oriMsgMqs.length);
         
        GlobalParas.oriMsgPoolSize = cfg.getIntProperty("oriMsgPoolSize");
        GlobalParas.oriMsgPool = new ArrayBlockingQueue(oriMsgPoolSize);
        GlobalParas.localPath = cfg.getProperty("localPath");
        GlobalParas.packPath = cfg.getProperty("packPath");
        if (!localPath.endsWith("/")) {
            localPath = localPath + "/";
        }
        if (!packPath.endsWith("/")) {
            packPath = packPath + "/";
        }
        GlobalParas.writeTimeOut = cfg.getIntProperty("writeTimeOut");
        GlobalParas.writeFileSize = cfg.getIntProperty("writeFileSize");
        GlobalParas.writeThreadSize = cfg.getIntProperty("writeThreadSize");

        GlobalParas.intervalSec = cfg.getIntProperty("intervalSec");
        GlobalParas.delaySec = cfg.getIntProperty("delaySec");

        log.info("the nameServer is " + nameServer + ",ifFromTimeStamp2Consumer is " + ifFromTimeStamp2Consumer + ",consumeTimeStamp is " + consumeTimeStamp
                + ",oriMsgMqs is " + Arrays.toString(oriMsgMqs) + ",oriMsgPoolSize is " + oriMsgPoolSize + ",localPath is " + localPath
                + ",packPath is " + packPath + ",writeTimeOut is " + writeTimeOut
                + ",writeFileSize is " + writeFileSize + ",writeThreadSize is " + writeThreadSize + ",intervalSec is " + intervalSec + ",delaySec is " + delaySec);
        spm = new SpeedMonitor(SpeedMonitor.PrintType.CUSTOM, GlobalParas.intervalSec, GlobalParas.delaySec);
        spm.setPrinter(new SpeedMonitor.Printer() {
            public Logger logger = Logger.getLogger(SpeedMonitor.class.getName());

            @Override
            public void print(String key, String value) {
                if (!"0".equals(value)) {
                    logger.info(key + " " + value);
                }
            }

            @Override
            public void print(Map<String, String> map) {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    print(key, value);
                }
            }
        });
        spm.start();
    }
}
